//
//  HWVIPController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/26.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWVIPController: HWBaseViewController {
    var statusBarStyle:UIStatusBarStyle = .default
    var tableView:UITableView!
    let tableViewProtocol = HWVIPProtocol()
    var buyButton:UIButton!
    var httpManager = HWVIPHttpManager()
    var navRightButton: UIButton!
    var itemId = ""
    var shuoming = ""
    override var preferredStatusBarStyle: UIStatusBarStyle {
        let _ = super.preferredStatusBarStyle
        return self.statusBarStyle
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.statusBarStyle = .lightContent
        setNeedsStatusBarAppearanceUpdate()
     
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addAction()
        
    }

}

extension HWVIPController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "游享会员"
        navigationView.titleLabel.textColor = .white
        navigationView.backButton.setImage(UIImage(named: "whiteNavBack"), for: .normal)
        view.backgroundColor = .colorWithHexString(hexString: "#272727")
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never

        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -120*widthScale
        
        buyButton = UIButton(type: .custom)
        buyButton.frame = CGRect(x: 0, y: 0, width: 339*widthScale, height: 48*widthScale)
        buyButton.layer.cornerRadius = 24*widthScale
        buyButton.layer.masksToBounds = true
        buyButton.setTitle("立即购买", for: .normal)
        buyButton.titleLabel?.font = .boldSystemFont(ofSize: 16*widthScale)
        buyButton.addTarget(self, action: #selector(buyButtonClick(_:)), for: .touchUpInside)
        view.addSubview(buyButton)
        UIColor.gradientColor(view: buyButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        navRightButton = UIButton(type: .custom)
        navRightButton.addTarget(self, action: #selector(navRightButtonClick(_:)), for: .touchUpInside)
        navRightButton.setImage(UIImage(named: "vipNavRight"), for: .normal)
        navigationView.addSubview(navRightButton)
    }
    
    private func initFrame() -> Void {
        
        navRightButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.width.height.equalTo(23*widthScale)
            make.centerY.equalTo(navigationView.titleLabel)
        }
        
        buyButton.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.height.equalTo(48*widthScale)
            make.bottom.equalTo(-53*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
            make.bottom.equalTo(buyButton.snp.top).offset(-18*widthScale)
        }
    }
}

extension HWVIPController {
    func loadData() -> Void {
        httpManager.getVIPInfo { model in
            self.tableViewProtocol.model = model
            if model.goods.count > 0 {
                model.goods.first?.isSelected = true
                self.itemId = model.goods.first?.id ?? ""
            }
            self.shuoming = model.jieshao
            self.tableView.reloadData()
        } failure: { error in
            
        }

    }
}

extension HWVIPController {
    func payButtonClick(_ type:String) -> Void {
        self.httpManager.getVIPPayInfo(id: self.itemId, type: type) { urlString in
            let webVC = XMGWebViewController()
            webVC.url = URL(string: urlString)
            self.navigationController?.pushViewController(webVC, animated: true)
        } failure: { error in
            
        }
    }
}

extension HWVIPController {
    func addAction() -> Void {
        self.tableViewProtocol.clickItem = {
            id in
            self.itemId = id
        }
        
        self.tableViewProtocol.goToCenterClick = {
            self.navigationController?.pushViewController(HWVipCenterController(), animated: true)
        }
    }
    
    @objc func buyButtonClick(_:UIButton) -> Void {
        let actionSheet = UIAlertController(title: "请选择支付方式", message: "", preferredStyle: .actionSheet)
        let action = UIAlertAction(title: "微信", style: .default) { act in
            self.payButtonClick("2")
        }
        let action1 = UIAlertAction(title: "支付宝", style: .default) { act in
            self.payButtonClick("1")
        }
        let cancel = UIAlertAction(title: "取消", style: .cancel) { act in
            
        }
        actionSheet.addAction(action)
        actionSheet.addAction(action1)
        actionSheet.addAction(cancel)
        
        HWUniversal.getCurrentVc().present(actionSheet, animated: true)
    }
    
    @objc func navRightButtonClick(_:UIButton) -> Void {
        if shuoming.count > 0 {
            let alert = HWVIPAlertView()
            let htmlString:NSMutableAttributedString = (self.shuoming.toHtmlAttributedText() ?? NSMutableAttributedString())

            htmlString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 13*widthScale), range: NSMakeRange(0, htmlString.length))
            htmlString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#333333"), range: NSMakeRange(0, htmlString.length))
            
            let style = NSMutableParagraphStyle()
            style.lineSpacing = 5*widthScale
            style.lineBreakMode = NSLineBreakMode.byWordWrapping
            htmlString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, htmlString.length))
            
            
            alert.contentLabel.attributedText = htmlString
            alert.show()
        }
    }
}

